package ac.manchester.cs.afzal.autoservd.interactions;

import ac.manchester.cs.afzal.autoservd.vector_handler.Vector_Builder;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Interactions
{

    private String DISTINCT_PENN_LOCATION = "DISTINCT_PENN_LOCATION";
    private Vector<String> occurrence_values, occurrence_values_2,ontology_values,distinct_sen_ids, distinct_sen_ids_2;

    String[] Class =
    {
        "algorithms", "alignments", "analysis", "applications", "approaches",
        "data", "databases", "datasets", "files", "libraries", "methods",
        "models", "procedures", "programs", "records", "reports", "repositories",
        "resources", "sequences", "searches", "services", "softwares", "systems",
        "tools", "trees"
    };
    private Properties props = new Properties();
    private Vector_Builder vectorBuilder = new Vector_Builder();
    static private SQL_Handler_Interactions sqlHandler1;
    static private SQL_Handler_Interactions sqlHandler2;

    public Interactions()
    {
        try
        {
            this.init_database(2);
            props.load(new FileInputStream("AutoServD.properties"));
            DISTINCT_PENN_LOCATION = props.getProperty(DISTINCT_PENN_LOCATION);
        }
        catch (IOException ex)
        {
            Logger.getLogger(Interactions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Finds the class instances which co-appear in same sentence
     */
    public void class_overlap()
    {
        sqlHandler2.reset_tables(new String[]
                {
                    "__interactions_class_overlap"
                });

        occurrence_values = sqlHandler2.get_all_semantic_class_instances();

        String csv_occurrence_values = vectorBuilder.convert_vector_to_csv(occurrence_values);
        occurrence_values = sqlHandler1.get_occurrences_distinct_sen_ids(csv_occurrence_values);
        distinct_sen_ids = sqlHandler1.get_distinct_sen_ids();

        for (int m = 0; m < distinct_sen_ids.size(); m++)
        {
            String distinct_sentence_id_1 = distinct_sen_ids.elementAt(m).toString();

            for (int n = 0; n < distinct_sen_ids.size(); n++)
            {
                String distinct_sentence_id_2 = distinct_sen_ids.elementAt(n).toString();

                if ((distinct_sentence_id_1.equals(distinct_sentence_id_2)) && !(m == n))
                {
                    String[] doc_table1 =
                    {
                        "Term1", "Term2", "Sentence"
                    };
                    String[] doc_table_value1 =
                    {
                        occurrence_values.elementAt(m).toString(),
                        occurrence_values.elementAt(n).toString(),
                        distinct_sentence_id_1
                    };
                    sqlHandler2.insert_table("__interactions_class_overlap", doc_table1, doc_table_value1);
                }
            }
        }
        sqlHandler1.close_db();
        sqlHandler2.close_db();

    }

    /**
     * Finds the class instances which co-appear with other significant terms
     * These siginificant terms are not the instances of Semantic Classes but appear
     * in Bioinformatics Corpus and recognized by C-value method of term recognition
     */
    public void class_overlap_terms()
    {
        sqlHandler2.reset_tables(new String[]
                {
                    "__interactions_class_overlap_terms"
                });
        occurrence_values = sqlHandler2.get_all_semantic_class_instances();

        String csv_occurrence_values = vectorBuilder.convert_vector_to_csv(occurrence_values);

        occurrence_values = sqlHandler1.get_occurrences_distinct_sen_ids(csv_occurrence_values);
        distinct_sen_ids = sqlHandler1.get_distinct_sen_ids();

        for (int i = 0; i < distinct_sen_ids.size(); i++)
        {
            String occurrence_value_1 = occurrence_values.elementAt(i).toString();
            occurrence_values_2 = sqlHandler1.get_occurrences_per_distinct_sentence_id(Integer.parseInt(distinct_sen_ids.elementAt(i).toString()));

            for (int j = 0; j < occurrence_values_2.size(); j++)
            {
                String occurrence_value_2 = occurrence_values_2.elementAt(j).toString();
                if ((occurrence_value_1.equals(occurrence_value_2)))
                {
                    String[] doc_table1 =
                    {
                        "Term1", "Term2", "Sentence"
                    };
                    String[] doc_table_value1 =
                    {
                        occurrence_value_1, occurrence_value_2,
                        distinct_sen_ids.elementAt(i).toString()
                    };
                    sqlHandler2.insert_table("__interactions_class_overlap_terms", doc_table1,
                            doc_table_value1);
                }
            }
        }
        sqlHandler1.close_db();
        sqlHandler2.close_db();

    }

    public void class_overlap_mygrid()
    {
        sqlHandler2.reset_tables(new String[]
                {
                    "__interactions_class_overlap_mygrid"
                });

        occurrence_values = sqlHandler2.get_all_semantic_class_instances();
        String csv_occurrence_values = vectorBuilder.convert_vector_to_csv(occurrence_values);

        occurrence_values = sqlHandler1.get_occurrences_distinct_sen_ids(csv_occurrence_values);
        distinct_sen_ids = sqlHandler1.get_distinct_sen_ids();

        ontology_values = sqlHandler2.get_all_ontology_values("mygrid_ontology");

        occurrence_values_2 = sqlHandler1.get_occurrences_distinct_sen_ids_for_ontology(ontology_values);
        distinct_sen_ids_2 = sqlHandler1.get_distinct_sen_ids_2();

        for (int i = 0; i < distinct_sen_ids.size(); i++)
        {
            String distinct_sen_id_1 = distinct_sen_ids.elementAt(i).toString();

            for (int j = 0; j < distinct_sen_ids_2.size(); j++)
            {
                String distinct_sen_id_2 = distinct_sen_ids_2.elementAt(j).toString();
                if (distinct_sen_id_1.equals(distinct_sen_id_2))
                {
                    String[] doc_table1 =
                    {
                        "Class_Term", "Mygrid_Term", "Sentence"
                    };
                    String[] doc_table_value1 =
                    {
                        occurrence_values.elementAt(i).toString(),
                        occurrence_values_2.elementAt(j).toString(), distinct_sen_id_1
                    };
                    sqlHandler2.insert_table("__interactions_class_overlap_mygrid", doc_table1,
                            doc_table_value1);
                }
            }
        }
        sqlHandler1.close_db();
        sqlHandler2.close_db();

    }

    public void class_overlap_descriptors()
    {
        sqlHandler2.reset_tables(new String[]
                {
                    "__interactions_class_overlap_descriptors"
                });

        occurrence_values = sqlHandler2.get_all_semantic_class_instances();
        String csv_occurrence_values = vectorBuilder.convert_vector_to_csv(occurrence_values);

        occurrence_values = sqlHandler1.get_occurrences_distinct_sen_ids(csv_occurrence_values);
        distinct_sen_ids = sqlHandler1.get_distinct_sen_ids();

        ontology_values = sqlHandler2.get_all_descriptor_values();

        occurrence_values_2 = sqlHandler1.get_occurrences_distinct_sen_ids_for_descriptors(ontology_values);
        distinct_sen_ids_2 = sqlHandler1.get_distinct_sen_ids_2();

        for (int i = 0; i < distinct_sen_ids.size(); i++)
        {
            String distinct_sen_id_1 = distinct_sen_ids.elementAt(i).toString();

            for (int j = 0; j < distinct_sen_ids_2.size(); j++)
            {
                String distinct_sen_id_2 = distinct_sen_ids_2.elementAt(j).toString();

                if (distinct_sen_id_1.equals(distinct_sen_id_2))
                {
                    String[] doc_table1 =
                    {
                        "Class", "Descriptor", "Sentence"
                    };
                    String[] doc_table_value1 =
                    {
                        occurrence_values.elementAt(i).toString(),
                        occurrence_values_2.elementAt(j).toString(), distinct_sen_id_1
                    };
                    sqlHandler2.insert_table("__interactions_class_overlap_descriptors", doc_table1,
                            doc_table_value1);
                }
            }
        }
        sqlHandler1.close_db();
        sqlHandler2.close_db();
    }

    protected void init_database(int count)
    {
        sqlHandler1 = new SQL_Handler_Interactions();
        sqlHandler1.initialize_parameters("jdbc:mysql://localhost:3306/BMC_2009", "root", "admin");
        sqlHandler1.connect_db();

        if (count == 2)
        {
            sqlHandler2 = new SQL_Handler_Interactions();
            sqlHandler2.initialize_parameters("jdbc:mysql://localhost:3306/BMC_2009_2", "root", "admin");
            sqlHandler2.connect_db();
        }

    }
    /*    public void argumet_overlap_descriptors(String mode)
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input;
    try
    {
    System.out.print("Do you want to delete previous interactions_class entries ? \n" +
    " Enter 'y' for yes, 'n' for no, or 'e' to exit : ");
    input = in.readLine();
    input = input.trim();

    if (input.equals("y"))
    {
    sqlHandler.delete_all(new String[]
    {
    "__interactions_class_overlap_descriptor_arg"
    });
    sqlHandler.reset_auto_inc(new String[]
    {
    "__interactions_class_overlap_descriptor_arg"
    });
    }
    else if (input.equals("e"))
    {
    System.exit(1);
    }
    }
    catch (Exception ex)
    {
    System.out.println(
    "Exception: Reading User's input : " + ex.getMessage());
    }

    descriptor_values = sqlHandler.get_descriptor_values();
    descriptor_ids = sqlHandler.descriptor_ids;

    for (int j = 0; j < descriptor_values.size(); j++)
    {
    String descriptor_value = descriptor_values.elementAt(j).toString();
    String descriptor_id = descriptor_ids.elementAt(j).toString();

    if (mode.equals("database"))
    {
    arguments_1 = sqlHandler.get_matching_argument_value(descriptor_value);
    occurrence_values_1 = sqlHandler.occurrence_values;

    for (int m = 0; m < arguments_1.size(); m++)
    {
    String term_value = occurrence_values_1.elementAt(m).toString();
    String argument_id = arguments_1.elementAt(m).toString();
    String[] doc_table1 =
    {
    "Class_Term", "Arg_ID", "Dic_ID", "Type"
    };
    String[] doc_table_value1 =
    {
    term_value, argument_id, descriptor_id,
    "DB_ACCESS"
    };
    sqlHandler.insert_table("__interactions_class_overlap_descriptor_arg", doc_table1, doc_table_value1);
    }

    }
    }

    }
     */
}
